package com.zmn.oms.admin.controller.conf.workturn.servcategory;

import com.zmn.base.product.common.dto.categ.CategServProductDRO;
import com.zmn.base.product.common.query.categ.ForeignCategServProductQuery;
import com.zmn.base.product.dubbo.interfaces.categ.serv.CategServProductListRemoteService;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.base.plat.engine.common.enums.PlatEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * 渠道BaseController
 *
 * @author xiangjunming
 * @since 2019 /08/19 19:30:36
 */
@Slf4j
public class PlatServCategoryBaseController {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    CategServProductListRemoteService categServProductListRemoteService;

    /**
     * 获取全部分类信息
     *
     * @param showType 产品分类 1后台 2前台
     * @return the list
     */
    @SuppressWarnings("Duplicates")
    protected List<KvDTO<Integer, String>> getAllChildCategory(int showType) {
        ForeignCategServProductQuery query = new ForeignCategServProductQuery();
        query.setShowType(showType);
        log.info("getAllChildCategory-request->{}", query);
        ResponseDTO<List<CategServProductDRO>> listResponseDTO = categServProductListRemoteService.listByQuery(query);
        log.info("getAllChildCategory--response-->{}", listResponseDTO);
        List<CategServProductDRO> categServProductList = listResponseDTO.getData();
        // 按服务分类分组
        Map<Integer, List<CategServProductDRO>> serviceCategoryMap = categServProductList.stream().collect(groupingBy(CategServProductDRO::getServCategId));
        List<KvDTO<Integer, String>> ret = serviceCategoryMap.values().stream().map(serviceCategory -> {
            // 按一级分类分组
            Map<Integer, List<CategServProductDRO>> categoryOneMap =
                    serviceCategory.stream().collect(groupingBy(CategServProductDRO::getCategOneId));

            List<KvDTO> categoryOneList = categoryOneMap.values().stream().map(categoryOne -> {
                // 二级分类KvDTO
                List<KvDTO> categList =
                        categoryOne.stream().map(p -> new KvDTO(p.getCategId(), p.getCategName())).collect(Collectors.toList());
                // 一级分类KvDTO
                KvDTO categOne = new KvDTO<>(categoryOne.get(0).getCategOneId(), categoryOne.get(0).getCategOneName());
                categOne.setChildren(categList);
                return (KvDTO<Integer, String>) categOne;
            }).collect(Collectors.toList());

            // 服务分类KvDTO
            KvDTO dto = new KvDTO<>(serviceCategory.get(0).getServCategId(), serviceCategory.get(0).getServCategName());
            dto.setChildren(categoryOneList);
            return (KvDTO<Integer, String>) dto;
        }).collect(Collectors.toList());

        return ret;
    }

    protected Integer getRealShowPlat(Integer plat) {
        boolean zmnPlat = true;//StaffAdminService.isPermitted("/plat/serv/category/zmnServCategory.action");
        boolean yeyxPlat = true;//StaffAdminService.isPermitted("/plat/serv/category/yeyxServCategory.action");
        boolean cnhbPlat = true;//StaffAdminService.isPermitted("/plat/serv/category/cnhbServCategory.action");
        boolean ybwxPlat = true;//StaffAdminService.isPermitted("/plat/serv/category/ybwxServCategory.action");

        boolean flag = false;
        Integer realPlat = 0;

        if (Objects.equals(plat, PlatEnum.ZMN.value) && zmnPlat) {
            realPlat = PlatEnum.ZMN.value;
        } else if (Objects.equals(plat, PlatEnum.YEYX.value) && yeyxPlat) {
            realPlat = PlatEnum.YEYX.value;
        } else if (Objects.equals(plat, PlatEnum.CNHB.value) && cnhbPlat) {
            realPlat = PlatEnum.CNHB.value;
        } else if (Objects.equals(plat, PlatEnum.YBSH.value) && ybwxPlat) {
            realPlat = PlatEnum.YBSH.value;
        }

        if (Objects.equals(realPlat, 0)) {
            if (zmnPlat) {
                return PlatEnum.ZMN.value;
            }
            if (yeyxPlat) {
                return PlatEnum.YEYX.value;
            }
            if (cnhbPlat) {
                return PlatEnum.CNHB.value;
            }
            if (ybwxPlat) {
                return PlatEnum.YBSH.value;
            }
        }

        return realPlat;
    }

    /**
     * 获取登录用户
     *
     * @return
     */
    public LoginStaff getMcStaff() {
        return StaffAdminService.currentStaff();
    }
}